<?php
namespace admin\controller;

use admin\model\UserModel;
use admin\model\PasswordHash;

class UserController extends Controller{
    
    private $user = null;

    public function __construct(){
        $this->user = new UserModel();
    }
    //查找指定id的用户并调用viwe显示
    public function getUser($params){
        $this->setData($this->user->getUser($params['id']));
        return $this->view('index');
    }
    //查询数据库中的所有用户信息并调用viwe显示
    public function getUserAll(){
        return $this->view('index');        //渲染页面
    }
    //查询所有用户的信息以 JSON 形式返回
    public function getUsersJson($params){
        $data = $this->user->getUserAll($params);
        $count_data = $this->user->getUserCount(array('name'=>$params['name']));
        $data_json = [];
        $data_json['code'] = 0;
        $data_json['msg'] = '';
        $data_json['count'] = $count_data['count'];
        $data_json['data'] = $data;
        return json_encode($data_json);
    }
    /**
     * 冻结用户
     * 传入后台冻结操作
     */
    public function frozen($params){
        $action = array('frozen'=>0);
        $data  =$this->user->updateUser(array_merge($action,$params));
        return json_encode($data);
    }
    /**
     * 修改用户权限
     */
    public function editUser($params){
        $data = $this->user->updateUser($params);
        return json_encode($data);
    }
    /**
     * 删除一个用户
     */
    public function delUser($params){
        $data = $this->user->delUser($params);
        return json_encode($data);
    }
    /**
     * 添加一个用户
     */
    public function addUser($params){
        $data = [];
        $check = $this->user->getUserName(array('name'=>$params['name']));
        if($check){
            $data['code'] = 2;
        }else{
            $params['create_time'] = time();
            $data = $this->user->addUser($params);
        }
        return json_encode($data);
    }
    /**
     * 修改管理员的信息
     */
    public function editManager($params){
        $data = $this->user->updateMan($params);
        return json_encode($data);
    }
    /**
     * 获取管理员的信息
     */
    public function getManAll(){
        session_start(); //开启SESSION
        $this->setData($this->user->getManInfo(array('id'=>$_SESSION['m_id'])));
        return $this->view('managerInfo');        //渲染页面
    }
    /**
     * 显示修改管理员的密码
     */
    public function getManagerPass($params){
        return $this->view('managerPass');
    }
    /**
     * 修改管理员密码
     */
    public function editManagerPass($params){
        $data = $this->user->updateManagerPass($params);
        return json_encode($data);
    }

}
